<?php

class MySQL implements DBManager {

	private $dbLink				= 0;

	public function __construct() {
	
		global $DB_CONFIG;

		$this->connect();
		$this->select( $DB_CONFIG['name'] );
	
	}

	public function __destruct() {

		$closeDbResult	= @mysql_close( $this->dbLink );

		if ( !$closeDbResult ) {
			throw new Exception( "Unable to close connexion with MySQL server", 0 );
		}
	
	}

	private function connect() {
		
		global $DB_CONFIG;

		$host	= $DB_CONFIG["host"];
		$user	= $DB_CONFIG["user"];
		$pass	= $DB_CONFIG["pass"];
		
		$this->dbLink	= @mysql_connect( $host, $user, $pass );
		
		if ( !$this->dbLink ) {
			throw new Exception( "Connexion with MySQL server faild. Login or password must be wrong", 0 );
		}

		$this->query( "SET SESSION lower_case_table_names = 0" );
		$this->query( "SET NAMES '" . $DB_CONFIG['charset'] . "'" );
		
		return true;
		
	}

	private function select( $database ) {

		if ( ! @mysql_select_db( $database, $this->dbLink ) ) {
			throw new Exception( "Connexion with MySQL server faild. Database '$database' does not exist", 0 );
		}
		
		return true;

	}
	
	public function query( $query ) {
	
		if ( in_array( substr( $query, 0, 6 ), array( "UDAPTE","DELETE" ) ) ) {
			$e	= new Exception(  $query, 0 );
		}
		
		return @mysql_query( $query, $this->dbLink );
	
	}

	public function fetch( $ressource ) {
	
		return @mysql_fetch_array( $ressource );
		
	}
	
	public function getError() {
	
		return mysql_error( $this->dbLink );
	
	}
	
	public function count( $ressource ) {
	
		return @mysql_num_rows( $ressource );
		
	}
	
	public function getInsertedId() {
	
		return @mysql_insert_id( $this->dbLink );
		
	}
	
}


?>